const Goods = require("../model/goods.model");
class GoodsService {
  async crateGoods(obj) {
    const res = await Goods.create(obj);
    console.log("---", res.toJSON(), "toJson");
    return res;
  }
  async updateRemark(remark){
        console.log(remark,'remark')
      await Goods.update(remark,{where:{}})
    
  }
  async updateGoods(obj,id) {

    const res = await Goods.update(obj, { where: { id } });

    return res[0];
  }
  async deleteGoods(id){
      await Goods.destroy({where:{id}})
  }
  async updateOneGoods(ctx){
   

      let {id,bill} = ctx.request.body
      let table = ''
      console.log(id,bill,'obj1')
      for(var i =0;i<bill.length;i++){
          table = 'table_' + id
            await Goods.update({
            [table]:bill[i].goods_num,
              },{
               where:{
              goods_name:bill[i].goods_name,
              
          }
      })
      }
    
  }
  async updateOrder(id,){
      let table = 'table_' + id
      let remark = 'remark_' + id
      let obj = {[table]:0,[remark]:''}
  
      const res = await Goods.update(obj,{where:{}})
      return res[0]
  }
  async removeGoods(id) {
    const res = await Goods.destroy({ where: { id } });
    console.log(res, "res");
    return res;
  }
  async destoreGoods(id) {
    const res = await Goods.destroy({ where: { id } });
    console.log(res, "res");
  }
  async restoreGoods(id) {
    const res = await Goods.restore({ where: { id } });
    console.log(res, "res");
  }
  async findGoodsAll() {
    const { rows, count } = await Goods.findAndCountAll();

    return {
      count,
      list: rows,
    };
  }
}
module.exports = new GoodsService();
